package com.chlitina.store.common.oss;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.fileupload.FileItem;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;
import com.chlitina.store.common.config.Global;

public class OSSServer {
	
	private static String accessKeyId = Global.getConfig("oss.accessKeyId");
	private static String accessKeySecret = Global.getConfig("oss.accessKeySecret");
	private static String bucketName = Global.getConfig("oss.bucketName");
	
	/**
	 * getOSSClient.
	 */
	public static OSSClient getOSSClient() {
		 OSSClient client = new OSSClient(accessKeyId, accessKeySecret);
		 return client;
	}
	
	/**
	 * save.
	 */
	public static String save(MultipartFile file) throws Exception{
		OSSClient client = getOSSClient();
		InputStream content = file.getInputStream();
		 // 创建上传Object的Metadata
        ObjectMetadata meta = new ObjectMetadata();
        String key =  java.util.UUID.randomUUID().toString() + "." + getExtension(file.getOriginalFilename());
        // 必须设置ContentLength
        meta.setContentLength(file.getSize());
	      // 上传Object.
        PutObjectResult result = client.putObject(bucketName, key, content, meta); 
        return key;
	}
	/**
	 *delete.
	 */
	public static void delete(String key) throws Exception{
		OSSClient client = getOSSClient();
		client.deleteObject(bucketName, key);
	}
	/** 
     * 获取扩展名的方法 
     */  
    private static String getExtension(String fileName) {  
        return fileName.substring(fileName.lastIndexOf(".") + 1);  
    }  
	/**
	 * save.
	 */
	public static String save(FileItem file) throws Exception{
		OSSClient client = getOSSClient();
		InputStream content = file.getInputStream();
		 // 创建上传Object的Metadata
        ObjectMetadata meta = new ObjectMetadata();
        String key =  java.util.UUID.randomUUID().toString() + "." + getExtension(file.getName());
        // 必须设置ContentLength
        meta.setContentLength(file.getSize());
	      // 上传Object.
        PutObjectResult result = client.putObject(bucketName, key, content, meta); 
        return key;
	}
	
	 public static void main(String[] args) {
		 try{
			 // 初始化一个OSSClient
	        OSSClient client = getOSSClient();

	        // 下面是一些调用代码...

	        File file = new File("d:/temp/photo.jpg");
	        InputStream content = new FileInputStream(file);

	        // 创建上传Object的Metadata
	        ObjectMetadata meta = new ObjectMetadata();

	        // 必须设置ContentLength
	        meta.setContentLength(file.length());
	        // 上传Object.
//	        PutObjectResult result = client.putObject(bucketName, "1234567890.jpg", content, meta); 
//	        System.out.println(result.toString());
	        OSSServer.delete("1234567890");
		 }catch(Exception e ){
			 e.printStackTrace();
		 }
	}
}
